package com.neusoft.elmboot.config;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import com.neusoft.elmboot.po.User;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

@Component
public class TokenConfig {
    private static Map<String, User> tokenCache = new HashMap<>();


    public static String createToken(User user) {
        String token = UUID.randomUUID().toString();
        tokenCache.put(token, user);
        return token;
    }

    public static User getLoginUser(HttpServletRequest request) {
        // 获取请求携带的令牌
        String token = getToken(request);
        if (token != null) {
            User user = tokenCache.get(token.replaceAll("\"", ""));
            return user;
        }
        return null;
    }

    private static String getToken(HttpServletRequest request) {
        String token = request.getHeader("token");
        return token;
    }


}
